package com.tm.media.utils.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Ly
 * @Data 2023/2/21 14:24
 * @ClassName SendController
 */
public class SendController {
    public  String LoginSendSms(String phoneNumber){
                DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5t7p1YhYLCs6EQbdPgKS", "Y1xKh1agHErlUDBOvC80rKGVNujoLk");
                /** use STS Token
                   DefaultProfile profile = DefaultProfile.getProfile(
                 "<your-region-id>",           // The region ID
                 "<your-access-key-id>",       // The AccessKey ID of the RAM account
                 "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
                 "<your-sts-token>");          // STS Token
                 **/
                IAcsClient client = new DefaultAcsClient(profile);
                SendSmsRequest request = new SendSmsRequest();
                request.setSignName("刘越的博客");
                request.setTemplateCode("SMS_270950046");
                request.setPhoneNumbers(phoneNumber);
                String substring = String.valueOf(Math.random()).substring(2, 8);
                request.setTemplateParam("{\"code\":\""+substring+"\"}");
                try {
                    SendSmsResponse response = client.getAcsResponse(request);
                    System.out.println(new Gson().toJson(response));
                } catch (ServerException e) {
                    e.printStackTrace();
                } catch (ClientException e) {
                    System.out.println("ErrCode:" + e.getErrCode());
                    System.out.println("ErrMsg:" + e.getErrMsg());
                    System.out.println("RequestId:" + e.getRequestId());
                }
                return substring;
    }
}
